<?php

namespace app\common\repositories\property\auth;

use app\helper\TempData;
use app\common\repositories\BaseRepository;
use app\common\dao\property\auth\PropertyAuthAccessDao;

/**
 * Class PropertyAuthAccessRepository
 *
 * @package app\common\repositories\property\auth
 * @mixin PropertyAuthAccessDao
 */
class PropertyAuthAccessRepository extends BaseRepository
{

    public function __construct(PropertyAuthAccessDao $dao)
    {
        $this->dao = $dao;
    }

    public function getPropertyList(array $where, $page, $limit)
    {
        $query = $this->dao->search($where);
        $count = $query->count();
        $list = $query->page($page, $limit)
            ->with([
                'roleInfo' => function ($query) {
                    $query->field('id,name');
                    $query->bind(['role_name' => 'name']);
                },
                'propertyInfo' => function ($query) {
                    $query->field('id,name');
                    $query->bind(['property_name' => 'name']);
                }
            ])
            ->order('id', 'desc')
            ->select();
        return compact('list', 'count');
    }


    public function getPropertyIdsByUserId(int $userId)
    {
        return TempData::remember('property_user_all_property_id_' . $userId, function () use ($userId) {
            $propertyIds = $this->dao->getSearch([
                'user_id' => $userId
            ])->column('property_id');
            return $propertyIds;
        }, 10);
    }

}
